package com.example.smartcity_40

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.example.smartcity_40.databinding.ActivityIntroDuctionBinding
import com.example.smartcity_40.tool.IntroductionAdapter
import com.example.smartcity_40.tool.jump
import com.example.smartcity_40.tool.tool
import com.example.smartcity_40.tool.viewBinding

class IntroDuctionActivity : AppCompatActivity() {
    private val vb by viewBinding(ActivityIntroDuctionBinding::inflate)
    private val iconArray = intArrayOf(R.drawable.page_d1, R.drawable.page_d2, R.drawable.page_d3)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(vb.root)
        itemDian(0)
        val sharedPreferences = getSharedPreferences("IntroDuction", Context.MODE_PRIVATE)
        val isFirstLaunch = sharedPreferences.getBoolean("isFirstLaunch", true)
        if (isFirstLaunch) {
            tool.apply {
                set("server", "124.93.196.45")
                set("port", "10001")
            }
            vb.Introduction.adapter = IntroductionAdapter(iconArray, this)
            sharedPreferences.edit().putBoolean("isFirstLaunch", false).apply()
        } else {
            jump(MainActivity::class.java)
            finish()
        }
        vb.Introduction.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                itemDian(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
    }

    private fun itemDian(position: Int) {
        val dots = arrayListOf(vb.dot1, vb.dot2, vb.dot3)
        for (i in dots.indices) {
            dots[i].setBackgroundResource(
                    if (i == position) R.drawable.splash_dot_active
                    else R.drawable.splash_dot
            )
        }
    }
}